﻿using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using LayerEntity.Sistema;

namespace System2012.Listados
{
    public partial class frmListadoPersona : DevExpress.XtraEditors.XtraForm
    {
        public string idPer;
        public string full_name;
        int indexRow;
        DataTable dtbPersona;
        public frmListadoPersona()
        {
            this.InitializeComponent();
            if (!this.BGWArea.IsBusy)
            {
                this.BGWArea.RunWorkerAsync();
            }
        }

        #region Metodos
        private void Seleccionar()
        {
            this.idPer = this.dtbPersona.Rows[this.indexRow]["id_persona"].ToString();
            this.full_name = this.dtbPersona.Rows[this.indexRow]["fullname_persona"].ToString();
            this.Close();
        }

        #endregion

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (this.gridView1.RowCount > 0)
                {
                    if (this.dtbPersona.Rows.Count > 0)
                    {
                        this.gridControl1.Focus();
                    }
                }
            }
        }

        private void BGWArea_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = System2012.SystemUtils.RecordManagerCommonList.GetAreas();
        }

        private void BGWArea_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.CmbArea.Properties.DataSource = e.Result;
            if (!this.BGWListadoPersonal.IsBusy)
            {
                this.BGWListadoPersonal.RunWorkerAsync();
            }
        }

        private void BGWListadoPersonal_DoWork(object sender, DoWorkEventArgs e)
        {
            ESistema obj = new ESistema();
            StringBuilder cadena = new StringBuilder();
            cadena.Append("where (doc_persona || fullname_persona || des_area) ILIKE ");
            cadena.Append("'%");
            cadena.AppendFormat("{0}%'", this.txtFiltro.Text);
            obj.Condicion = cadena.ToString();
            e.Result = System2012.SystemUtils.RecordManagerCommonList.GetListadoPersonal(obj);
        }

        private void BGWListadoPersonal_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.dtbPersona = (DataTable)e.Result;
            this.gridControl1.DataSource = e.Result;
        }

        private void txtFiltro_EditValueChanged(object sender, EventArgs e)
        {
        }

        private void txtFiltro_TextChanged(object sender, EventArgs e)
        {
            if (!this.BGWListadoPersonal.IsBusy)
            {
                this.BGWListadoPersonal.RunWorkerAsync();
            }
        }

        private void frmListadoPersona_Load(object sender, EventArgs e)
        {
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.indexRow = e.FocusedRowHandle;
        }

        private void gridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            this.Seleccionar();  
        }

        private void gridControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.Seleccionar();
            }
        }

        private void txtFiltro_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.gridView1.RowCount > 0)
                {
                    if (this.dtbPersona.Rows.Count > 0)
                    {
                        this.gridControl1.Focus();
                    }
                }
            }
        }

        private void CmbArea_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void CmbArea_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Left)
            {
                this.txtFiltro.Focus();
            }
        }

        private void frmListadoPersona_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
                //this.Dispose();
            }
        }

        private void frmListadoPersona_Shown(object sender, EventArgs e)
        {
            txtFiltro.Focus();
        }
    }
}